﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Razre.Base;
using Microsoft.Practices.Composite.Presentation.Commands;
using NavigationMVVMDemo.Models;
using NavigationMVVMDemo.Events;
using Razre;

namespace NavigationMVVMDemo.ViewModels
{
    public class PhoneNumberViewModel : BaseViewModel
    {
        public PhoneNumberViewModel()
        {
            SaveCommand = new DelegateCommand<object>(OnSave);
            CancelCommand = new DelegateCommand<object>(OnCancel);
        }

        private PhoneNumberModel _phoneNumber;
        public PhoneNumberModel PhoneNumber
        {
            get
            {
                return _phoneNumber;
            }
            set
            {
                _phoneNumber = value;
                OnPropertyChanged("PhoneNumber");
            }
        }

        public event EventHandler<PhoneNumberFinishedEventArgs> OnPhoneNumberSaved;

        public DelegateCommand<object> SaveCommand { get; set; }
        public DelegateCommand<object> CancelCommand { get; set; }

        private void OnSave(object parameter)
        {
            if (OnPhoneNumberSaved != null)
            {
                PhoneNumberFinishedEventArgs args = new PhoneNumberFinishedEventArgs();
                args.IsSaved = true;
                args.PhoneNumber = PhoneNumber;

                OnPhoneNumberSaved(this, args);
            }

            NavigationManager.Back();
        }

        private void OnCancel(object parameter)
        {
            if (OnPhoneNumberSaved != null)
            {
                PhoneNumberFinishedEventArgs args = new PhoneNumberFinishedEventArgs();
                args.IsSaved = false;
            }

            NavigationManager.Back();
        }
    }
}
